﻿// Copyright (c) 2012 Mokah Nnamezie & Andrew Cooper
// Neo4Net graph database - http://neo4net.codeplex.com
//
// This file is part of Neo4Net - a .Net port of the Neo4j graph database
//
// Neo4Net is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

namespace Neo4Net.Kernel.Impl.Core
{
    using System;
    using System.IO;
    using Neo4Net.Kernel.Impl.Nioneo.Store;
    using Neo4Net.Kernel.Impl.Util;

    public partial class JumpingFileSystemAbstraction : IFileSystemAbstraction
    {
        private readonly int sizePerJump;

        public JumpingFileSystemAbstraction(int sizePerJump)
        {
            this.sizePerJump = sizePerJump;
        }

        public Stream Open(string fileName, FileAccess access)
        {
            var stream = File.Open(fileName, FileMode.OpenOrCreate, access);
            if (fileName.EndsWith("neostore.nodestore.db") ||
                    fileName.EndsWith("neostore.relationshipstore.db") ||
                    fileName.EndsWith("neostore.propertystore.db") ||
                    fileName.EndsWith("neostore.propertystore.db.strings") ||
                    fileName.EndsWith("neostore.propertystore.db.arrays"))
            {
                return new JumpingFileStream(fileName, FileMode.OpenOrCreate, access, this, RecordSizeFor(fileName));
            }
            return stream;
        }

        public Stream Create(string fileName)
        {
            return Open(fileName, FileAccess.ReadWrite);
        }

        public bool FileExists(string fileName)
        {
            return File.Exists(fileName);
        }

        public long GetFileSize(string fileName)
        {
            return new FileInfo(fileName).Length;
        }

        public bool DeleteFile(string fileName)
        {
            return FileUtils.DeleteFile(new FileInfo(fileName));
        }

        public bool RenameFile(string from, string to)
        {
            return FileUtils.RenameFile(new FileInfo(from), new FileInfo(to));
        }

        public void CopyFile(string from, string to)
        {
            FileUtils.CopyRecursively(new FileInfo(from), new FileInfo(to));
        }

        public FileLock TryLock(string fileName, Stream channel)
        {
            return FileLock.GetOsSpecificFileLock(fileName, channel);
        }

        private int RecordSizeFor(string fileName)
        {
            if (fileName.EndsWith("nodestore.db"))
            {
                return NodeStore.RecordSize;
            }
            else if (fileName.EndsWith("relationshipstore.db"))
            {
                return RelationshipStore.RecordSize;
            }
            else if (fileName.EndsWith("propertystore.db.strings") || fileName.EndsWith("propertystore.db.arrays"))
            {
                return AbstractDynamicStore.GetRecordSize(PropertyStore.DefaultDataBlockSize);
            }
            else if (fileName.EndsWith("propertystore.db"))
            {
                return PropertyStore.RecordSize;
            }
            throw new ArgumentException(null, "fileName");
        }
    }
}
